#include "dominion.h"
#include "testutils.h"
#include <stdio.h>
#include <stdlib.h>

// This unit test was added after it was found (via random testing, confirmed
// with code inspection) that the sea hag would not discard itself when
// played. All combinations of card positions are tested. 

// The test was expanded when code inspection revealed the possibility that 
// the wea hag card may not handle other players having empty decks.

void testSeahagEffect ( int args[MAX_PLAYARGS], int cardtypes[MAX_PLAYARGS],
                        carddesc_t desc, struct gameState *g,
                        expectedState_t *expected )
{
    bool otherPlayersNeedShuffle = args[4] != 0;
    
    pass_printf ("%s: TEST playing %s ", __func__, desc[0]);
    if (otherPlayersNeedShuffle)
        pass_printf ("(other players will need shuffle.)\n");
    else
        pass_printf ("(other players won't need shuffle.)\n");
    
    int player = g->whoseTurn;
    
    if (otherPlayersNeedShuffle)
    {
        for (int i = 0; i < g->numPlayers; i++)
        {
            if (i == player)
                continue;
            
            while (g->deckCount[i])
            {
                g->discard[i][g->discardCount[i]++] = g->deck[i][--g->deckCount[i]];
            }
        }
    }
    
    testPlayCard (args[0], args[1], args[2], args[3], g, expected, false);
    
    for (int i = 0; i < g->numPlayers; i++)
    {
        if (i == player)
            continue;
            
        assert_equal (  g->deckCount[i], 5,
                        "verifying player %d's deck pile size", i );
        assert_equal (  g->discardCount[i], 1,
                        "verifying player %d's discard pile size", i );
    }
    
    testEndTurn (g, expected);
    
    // TODO: use the expectedState system for all players
    for (int i = 0; i < g->numPlayers; i++)
    {
        int expectedCurseCount = i != player;
        int expectedSeaHagCount = i == player;
        int expectedCardCount = expectedCurseCount + expectedSeaHagCount + 7 + 3;
        int actualCardCount = g->handCount[i] + g->deckCount[i] + g->discardCount[i];
        assert_equal (  actualCardCount, expectedCardCount,
                        "verifying player %d's total number of cards", i );
        assert_equal (  fullDeckCount (i, curse, g), expectedCurseCount,
                        "verifying player %d's number of curse cards", i );
        assert_equal (  fullDeckCount (i, copper, g), 7,
                        "verifying player %d's number of copper cards", i );
        assert_equal (  fullDeckCount (i, estate, g), 3,
                        "verifying player %d's number of estate cards", i );
    }
}

int main (int argc, char *argv[])
{
    parse_args (argc, argv);
    
    static playargs_t playargs =
    {
        {arg_handcard,  sea_hag, sea_hag},
        {arg_num,       0, 0},
        {arg_num,       0, 0},
        {arg_num,       0, 0},
        {arg_num,       0, 1} // otherPlayersNeedShuffle
    };
    
    run_cardtest (playargs, testSeahagEffect);
}
